#include <51func.h>
#define NUM 10000000
typedef struct shareRes_s {
    int num;
    pthread_mutex_t mutex;
} shareRes_t;
void *threadFunc(void *arg){
    shareRes_t *pshareRes = (shareRes_t *)arg;
    for(int i = 0; i < 10000000; ++i){
        pthread_mutex_lock(&pshareRes->mutex); // 临界区开始
        ++pshareRes->num;
        pthread_mutex_unlock(&pshareRes->mutex); // 临界区结束
    }
    pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
    shareRes_t shareRes;
    shareRes.num = 0;
    pthread_mutex_init(&shareRes.mutex,NULL); //动态初始化
    pthread_t tid;
    // gettimeofday
    struct timeval begTime,endTime;
    gettimeofday(&begTime,NULL);
    pthread_create(&tid,NULL,threadFunc,&shareRes); // shareRes_t * --> void *
    for(int i = 0; i < 10000000; ++i){
        pthread_mutex_lock(&shareRes.mutex); // 临界区开始
        ++shareRes.num;
        pthread_mutex_unlock(&shareRes.mutex); // 临界区结束
    }
    pthread_join(tid,NULL);
    // gettimeofday
    gettimeofday(&endTime,NULL);
    printf("total = %d\n", shareRes.num);
    printf("total time = %ld us\n", endTime.tv_usec - begTime.tv_usec + 1000000*(endTime.tv_sec - begTime.tv_sec));
    return 0;
}

